/***************************************************************
*  Name:  utils
*	   
*  Synopsis:  utility function
*    	    
*  Description:
*     	    
*  History:
*      	         dietrich - Feb 2, 2012:  Created.
*
***************************************************************/

/* $Id$ */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <gmp.h>
#include <time.h>


void memdumpex (char * func, char *name, unsigned char *mem, size_t n, size_t linelen)
{
	int i;
	printf("%s:%s: len %d\n",func,name,n);
	for ( i=0; i<n; i++) {
		if ((i % linelen) == 0)
			printf("%03x: ",i);
			
		printf("%02x",mem[i]);
		if ((i % linelen) == (linelen-1))
			printf("\n");
	}
	if ((i % linelen) != 0)
		printf("\n");
}

void memdump (char *name, unsigned char *mem, size_t n)
{
	int i;
	printf("dump of %s, len %d\n",name,n);
	for ( i=0; i<n; i++) {
		if ((i % 16) == 0)
			printf("%03x: ",i);
			
		printf("%02x",mem[i]);
		if ((i % 16) == 15)
			printf("\n");
	}
	if ((i % 16) != 0)
		printf("\n");
}



int save_data_file(char *name, void *buf, int size)
{
	int fd;
	int ret;
	fd=open(name, O_WRONLY|O_CREAT, 0666);
	if (fd<0) {
		printf("couldnt open file %s\n",name);
		return -1;
	}
	ret=write(fd, buf, size);
	if (ret<0) {
		close(fd);
		printf("couldnt write file %s\n",name);
		return -1;
		
	}
	if (ret!=size) {
		close(fd);
		printf("partial write %s\n",name);
		return -1;
		
	}

	close(fd);
	return 0;
}
